//
//  BLMyOrderController.m
//  Byelone
//
//  Created by 乔小乔 on 2021/2/5.
//

#import "BLMyOrderController.h"
#import "SPPageMenu.h"
#import "BLOrderPearlVC.h"

@interface BLMyOrderController ()<SPPageMenuDelegate>
@property (nonatomic, strong) SPPageMenu *pageMenu;
@property (nonatomic, strong) NSMutableArray *myChildViewControllers;
@property (nonatomic, strong) NSMutableArray *pageArray;
@property (nonatomic, assign) NSInteger currentIndex;
@property (nonatomic, strong) UIScrollView *scrollView;

@end

@implementation BLMyOrderController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationItem.title = @"收支明细";
    [self createUI];
}
- (void)createUI{
    [self.view addSubview:self.scrollView];
    [self.view addSubview:self.pageMenu];
    if ([BLUserManager shareUser].baseInfo.gender == 1) {//男
        self.selectIndex = self.selectIndex?0:1;
    }
    if (self.pageMenu.selectedItemIndex < self.myChildViewControllers.count) {
        UIViewController *childController = self.myChildViewControllers[self.pageMenu.selectedItemIndex];
        [self.scrollView addSubview:childController.view];
        childController.view.frame = CGRectMake(BL_WINDOW_WIDTH * self.pageMenu.selectedItemIndex, 0.0, BL_WINDOW_WIDTH, BL_CONTENT_HEIGHT - 48);
        self.scrollView.contentOffset = CGPointMake(BL_WINDOW_WIDTH * self.pageMenu.selectedItemIndex, 0.0);
        self.scrollView.contentSize = CGSizeMake(self.pageArray.count * BL_WINDOW_WIDTH, 0.0);
    }
}
#pragma mark - SPPageMenu的代理方法
- (void)pageMenu:(SPPageMenu *)pageMenu itemSelectedFromIndex:(NSInteger)fromIndex toIndex:(NSInteger)toIndex
{
    if (!self.scrollView.isDragging) {
        if (labs(toIndex - fromIndex) >= 2) {
            [self.scrollView setContentOffset:CGPointMake(BL_WINDOW_WIDTH * toIndex, 0) animated:NO];
        } else {
            [self.scrollView setContentOffset:CGPointMake(BL_WINDOW_WIDTH * toIndex, 0) animated:YES];
        }
    }
    if (self.myChildViewControllers.count <= toIndex) {return;}
    UIViewController *targetViewController = self.myChildViewControllers[toIndex];
    if ([targetViewController isViewLoaded]) return;
    targetViewController.view.frame = CGRectMake(BL_WINDOW_WIDTH * toIndex, 0.0, BL_WINDOW_WIDTH, BL_CONTENT_HEIGHT-48);
    [self.scrollView addSubview:targetViewController.view];
}
#pragma mark - get&&set
- (SPPageMenu *)pageMenu
{
    if (_pageMenu == nil) {
        _pageMenu = [SPPageMenu pageMenuWithFrame:CGRectMake(0, 0.0, BL_WINDOW_WIDTH, 40) trackerStyle:SPPageMenuTrackerStyleLineAttachment];
        _pageMenu.trackerWidth = 100;
        //去掉下划线
        _pageMenu.dividingLine.hidden = YES;
        
        //设置选中颜色
        _pageMenu.selectedItemTitleColor = [UIColor chColorWithHexString:@"#6B71F5"];
        //滑块颜色
        _pageMenu.tracker.backgroundColor = [UIColor chColorWithHexString:@"#6B71F5"];
        //未选中颜色
        _pageMenu.unSelectedItemTitleColor = [UIColor chColorWithHexString:@"#888888"];
        //字体大小
        _pageMenu.selectedItemTitleFont = [UIFont boldSystemFontOfSize:16];
        _pageMenu.unSelectedItemTitleFont = [UIFont systemFontOfSize:16];
        
        [_pageMenu setItems:self.pageArray selectedItemIndex:self.selectIndex];
        _pageMenu.permutationWay = SPPageMenuPermutationWayNotScrollAdaptContent;
        
        _pageMenu.delegate = self;
        _pageMenu.bridgeScrollView = self.scrollView;
    }
    return _pageMenu;
}
- (UIScrollView *)scrollView
{
    if (_scrollView == nil) {
        _scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0.0, 40, BL_WINDOW_WIDTH, BL_CONTENT_HEIGHT-48)];
        _scrollView.pagingEnabled = YES;
        _scrollView.showsHorizontalScrollIndicator = NO;
        _scrollView.showsVerticalScrollIndicator = NO;
    }
    return _scrollView;
}
- (NSMutableArray *)pageArray{
    if ((!_pageArray)) {
        if ([BLUserManager shareUser].baseInfo.gender == 1) {
            _pageArray = [NSMutableArray arrayWithArray:@[@"贝壳",@"珍珠"]];
        }else{
            _pageArray = [NSMutableArray arrayWithArray:@[@"珍珠",@"贝壳"]];
        }
        
    }
    return _pageArray;
}

- (NSMutableArray *)myChildViewControllers{
    if (!_myChildViewControllers) {
        _myChildViewControllers = [[NSMutableArray alloc]init];
        BLOrderPearlVC *pearlVC = [[BLOrderPearlVC alloc]init];
        pearlVC.giftType = 1;
        
        BLOrderPearlVC *shellVC = [[BLOrderPearlVC alloc]init];
        shellVC.giftType = 2;
        
        [_myChildViewControllers addObjectsFromArray:@[pearlVC,shellVC]];
    }
    return _myChildViewControllers;
}

@end
